﻿using Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace DAL
{
    public class FoodDAL
    {
        /// <summary>
        /// 依赖注入
        /// </summary>
        private readonly MyDbContext db;

        public FoodDAL(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 显示食谱类的所有信息
        /// </summary>
        /// <returns></returns>
        public IQueryable<FoodType> Getall()
        {
            try
            {
                return db.FoodType.AsQueryable();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 新增食谱信息
        /// </summary>
        /// <param name="info">食谱类</param>
        /// <returns></returns>
        public int Add(Food info)
        {
            try
            {
                db.Food.Add(info);
                return db.SaveChanges();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 显示+查询
        /// </summary>
        /// <param name="name">食谱标题</param>
        /// <param name="tid">食谱类</param>
        /// <returns></returns>
        public IQueryable<DTO> Show(string ?name,int tid=-1)
        {
            try
            {
                var list = from a in db.Food
                           join b in db.FoodType
                           on a.TId equals b.TId
                           select new DTO
                           {
                               Id = a.Id,
                               Name = a.Name,
                               Xiangqing = a.Xiangqing,
                               State = a.State,
                               TId = a.TId,
                               img = a.img,
                               Delete = a.Delete,
                               TName = b.TName,
                           };
                if (list != null)
                {
                    if (!string.IsNullOrEmpty(name))
                    {
                        list = list.Where(x=>x.Name.Contains(name));
                    }
                    if (tid != -1)
                    {
                        list = list.Where(x=>x.TId==tid);
                    }
                }
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
